package com.zdd.ehe.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.zdd.ehe.entity.Role;
import com.zdd.ehe.entity.User;
import com.zdd.ehe.result.Result;
import com.zdd.ehe.service.IRoleService;
import com.zdd.ehe.vo.RoleVo;
import com.zdd.ehe.vo.UserVo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.web.bind.annotation.*;
import org.springframework.stereotype.Controller;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author ZhuiZhu
 * @since 2023-03-13
 */
@Api(tags="角色管理模块")
@RestController
@RequestMapping("/role")
@Slf4j
public class RoleController {

    @Resource
    private IRoleService roleService;

    /**
     * Role列表转RoleVo列表，添加角色和权限
     * @param roleList
     * @return
     */
    public List<RoleVo> converList(List<Role> roleList){
        List<RoleVo> roleVoList = new ArrayList<>();
        for(Role role:roleList){
            RoleVo roleVo = converObject(role);
        }
        return roleVoList;
    }

    /**
     * Role对象转换成RoleVo对象
     * @param role
     * @return
     */
    public RoleVo converObject(Role role){
        RoleVo roleVo = new RoleVo();
        BeanUtils.copyProperties(role,roleVo);
        return roleVo;
    }

    @ApiOperation("分页获取角色接口")
    @PostMapping("/getallrolesbypage")
    public Result getAllRolesByPage(@RequestParam("page") Integer page,@RequestParam("pageSize") Integer pageSize){
        Page<Role> pagex = new Page<>(page,pageSize);
        Page<Role> pageResult = roleService.getAllRoleByPage(pagex);
        if(pageResult.getSize() == 0){
            return Result.error(4001,"无数据");
        }

        return Result.success("查询成功",pageResult);
    }

    @ApiOperation("删除角色接口")
    @PostMapping("/deleterolebyid")
    public Result deleteRoleById(@RequestParam("id") Integer id){
        Role role = roleService.selectRoleById(id);
        if(role == null){
            return Result.error(4001,"角色编号错误");
        }
        role.setStatus("0");
        int result = roleService.updateRoleById(role);
        if(result == -1){
            return Result.error(4002,"删除失败");
        }
        return Result.success("删除成功");
    }


    @ApiOperation("通过rolename获取角色列表接口")
    @GetMapping("/getrolebyrolename")
    public Result getRoleByRoleName(@RequestParam("roleName") String roleName){
        QueryWrapper wrapper = new QueryWrapper();
        wrapper.like("name",roleName);
        List<Role> roleList = roleService.list(wrapper);
        if(roleList.size() != 0){
            List<RoleVo> roleVoList = converList(roleList);
            Map<String,Object> data = new HashMap<>();
            data.put("userList",roleList);
            data.put("total",roleList.size());
            data.put("size",10);
            data.put("current",1);
            data.put("pages",1);
            return Result.success("查询成功!",data);
        }else{
            return Result.error(4001,"未查询到该角色");
        }
    }


    @ApiOperation("添加角色")
    @PostMapping("/addrole")
    public Result addRole(@RequestBody Role role){
        log.info("role: " + role);
        int r = roleService.addRole(role);
        if(r == 1){
            return Result.success("添加成功");
        }else {
            return Result.error(4002,"添加失败");
        }
    }

    @ApiOperation("获取所有角色")
    @GetMapping("/getAllRole")
    public Result getAllRole(){
        QueryWrapper<Role> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("status","1");
        List<Role> roleList= roleService.list(queryWrapper);
        List<Map<String,Object>> data = new ArrayList<>();
        for(Role role:roleList){
           Map<String,Object> map = new HashMap<>();
           map.put("id",role.getId());
           map.put("label",role.getName());
           data.add(map);
        }
        if(data.size() == 0){
            return Result.error("暂无数据");
        }
        return Result.success("查询成功",data);
    }

    @ApiOperation("修改角色信息")
    @PostMapping("/updaterole")
    public Result updateRole(@RequestBody Role role){
        //之前失误过（整张表都被改过）
        int role_update = roleService.updateRoleById(role);
        if(role_update == -1){
            return Result.error("修改失败");
        }
        return Result.success("修改成功");
    }

    @ApiOperation("重启角色接口")
    @PostMapping("/restartrolebyid")
    public Result restartRoleById(@RequestParam int id){
        Role role = roleService.getById(id);
        if(role == null){
            return Result.error(4001,"角色编号错误");
        }
        role.setStatus("1");
        int result = roleService.updateRoleById(role);
        if(result == -1){
            return Result.error(4002,"重新启用失败");
        }
        return Result.success("重新启用成功");
    }


}
